package com.patent.controller;

import com.patent.common.CreateOrderNum;
import com.patent.common.JsonUtil;
import com.patent.common.PatentConstant;
import com.patent.common.ResultCode;
import com.patent.common.ResultData;
import com.patent.domain.po.Invoice;
import com.patent.domain.po.InvoiceAmount;
import com.patent.service.InvoiceAmountService;
import com.patent.service.InvoiceService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发票相关的
 */
@Controller
@RequestMapping("/invoice")
public class InvoiceController {

	private static final Logger log = LoggerFactory.getLogger(InvoiceController.class);

	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private InvoiceAmountService invoiceAmountService;

	/**
	 * 添加发票
	 * invoiceType：发票类型 1：企业增值税普通发票 2：企业增值税专用发票
	 */
	@RequestMapping(value = "/addInvoice.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addInvoice(String userMobile, Double invoiceAmount, String invoiceTitle, String identifyNumber,
	                         String receiverName, String receiverMobile, String area, String detailAddress, Integer invoiceType,
	                         String openBlankName, String openBlankAccount, String registryPlaceAddress, String registryPlaceMobile){
		ResultData resultData = new ResultData();
		if(invoiceAmount == null){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("invoiceAmount is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		if(invoiceAmount <= 0){
			resultData.setCode(ResultCode.invoiceAmountNotThanZero.getCode());
			resultData.setMsg(ResultCode.invoiceAmountNotThanZero.getMessage());
			return JsonUtil.write2JsonStr(resultData);
		}
		if(StringUtils.isBlank(invoiceTitle)){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("invoiceTitle is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		if(StringUtils.isBlank(receiverName)){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("receiverName is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		if(StringUtils.isBlank(receiverMobile)){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("receiverMobile is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		if(StringUtils.isBlank(detailAddress)){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("detailAddress is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		if(invoiceType == null){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("invoiceType is null");
			return JsonUtil.write2JsonStr(resultData);
		}else{
			if(invoiceType == PatentConstant.SPECIAL){
				if(StringUtils.isBlank(openBlankName)){
					resultData.setCode(ResultCode.paramError.getCode());
					resultData.setMsg("openBlankName is null");
					return JsonUtil.write2JsonStr(resultData);
				}
				if(StringUtils.isBlank(openBlankAccount)){
					resultData.setCode(ResultCode.paramError.getCode());
					resultData.setMsg("openBlankAccount is null");
					return JsonUtil.write2JsonStr(resultData);
				}
				if(StringUtils.isBlank(registryPlaceAddress)){
					resultData.setCode(ResultCode.paramError.getCode());
					resultData.setMsg("registryPlaceAddress is null");
					return JsonUtil.write2JsonStr(resultData);
				}
				if(StringUtils.isBlank(registryPlaceMobile)){
					resultData.setCode(ResultCode.paramError.getCode());
					resultData.setMsg("registryPlaceMobile is null");
					return JsonUtil.write2JsonStr(resultData);
				}
			}
		}
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("userMobile", userMobile);
		InvoiceAmount ia = invoiceAmountService.selectByMap(queryMap);
		log.info("ia:" + JsonUtil.write2JsonStr(ia));
		if(ia != null){
			Double amount = ia.getAmount().doubleValue();
			log.info("start:" + (invoiceAmount.compareTo(amount) > 0));
			if(invoiceAmount.compareTo(amount) > 0){
				resultData.setCode(ResultCode.invoiceAmountNo.getCode());
				resultData.setMsg(ResultCode.invoiceAmountNo.getMessage());
				log.info("InvoiceController addInvoice:" + JsonUtil.write2JsonStr(resultData));
				return JsonUtil.write2JsonStr(resultData);
			}
		}else{
			resultData.setCode(ResultCode.invoiceAmountZero.getCode());
			resultData.setMsg(ResultCode.invoiceAmountZero.getMessage());
			log.info("InvoiceController addInvoice:" + JsonUtil.write2JsonStr(resultData));
			return JsonUtil.write2JsonStr(resultData);
		}
		Invoice invoice = new Invoice();
		invoice.setUserMobile(userMobile);
		invoice.setInvoiceAmount(invoiceAmount);
		invoice.setInvoiceTitle(invoiceTitle);
		invoice.setIdentifyNumber(identifyNumber);
		invoice.setReceiverMobile(receiverMobile);
		invoice.setReceiverName(receiverName);
		invoice.setArea(area);
		invoice.setDetailAddress(detailAddress);
		invoice.setInvoiceType(invoiceType);
		invoice.setOpenBlankName(openBlankName);
		invoice.setOpenBlankAccount(openBlankAccount);
		invoice.setRegistryPlaceAddress(registryPlaceAddress);
		invoice.setRegistryPlaceMobile(registryPlaceMobile);
		log.info("invoice request:" + JsonUtil.write2JsonStr(invoice));
		invoice.setInvoiceStatu(PatentConstant.HAS_SUBMIT);
		invoice.setSerialNumber(PatentConstant.INVOICE_FLAG + CreateOrderNum.getOrderNo());
		invoice.setValidFlag(PatentConstant.YES_FLAG);
		try {
			invoiceService.addInvoice(invoice);
			resultData.setCode(ResultCode.success.getCode());
			resultData.setMsg(ResultCode.success.getMessage());
		} catch (Exception e) {
			resultData.setCode(ResultCode.otherError.getCode());
			resultData.setMsg(ResultCode.otherError.getMessage());
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.info("InvoiceController addInvoice:" + JsonUtil.write2JsonStr(resultData));
		return JsonUtil.write2JsonStr(resultData);
	}

	/**
	 * 获取单个用户发票列表
	 */
	@RequestMapping(value = "/getInvoiceList.do", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getInvoiceList(String userMobile, @RequestParam(value="page", defaultValue="1") Integer page,
	                             @RequestParam(value="rows",defaultValue="10") Integer rows){
		ResultData resultData = new ResultData();
		try {
			List<Invoice> invoiceList = invoiceService.getInvoiceList(userMobile, page, rows);
			resultData.setCode(ResultCode.success.getCode());
			resultData.setMsg(ResultCode.success.getMessage());
			resultData.setResult(invoiceList);
		} catch (Exception e) {
			resultData.setCode(ResultCode.otherError.getCode());
			resultData.setMsg(ResultCode.otherError.getMessage());
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.info("InvoiceController getInvoiceList:" + JsonUtil.write2JsonStr(resultData));
		return JsonUtil.write2JsonStr(resultData);
	}

	/**
	 * 取消提交的发票
	 * @param serialNumber  流水号
	 */
	@RequestMapping(value = "/cancelInvoice.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String cancelInvoice(String userMobile, String serialNumber){
		ResultData resultData = new ResultData();
		if(StringUtils.isBlank(serialNumber)){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("serialNumber is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		try {
			resultData = invoiceService.cancelInvoice(userMobile, serialNumber);
		} catch (Exception e) {
			resultData.setCode(ResultCode.otherError.getCode());
			resultData.setMsg(ResultCode.otherError.getMessage());
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.info("InvoiceController cancelInvoice:" + JsonUtil.write2JsonStr(resultData));
		return JsonUtil.write2JsonStr(resultData);
	}

	/**
	 * 获取用户可开发票总额及最近一次开票信息
	 */
	@RequestMapping(value = "/getInvoiceInfo.do", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getInvoiceInfo(String userMobile){
		ResultData resultData = new ResultData();
		try {
			resultData = invoiceService.getInvoiceInfo(userMobile);
		} catch (Exception e) {
			resultData.setCode(ResultCode.otherError.getCode());
			resultData.setMsg(ResultCode.otherError.getMessage());
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.info("InvoiceController getInvoiceInfo:" + JsonUtil.write2JsonStr(resultData));
		return JsonUtil.write2JsonStr(resultData);
	}
}
